<?php

namespace App\Http\Controllers;

use Laravel\Lumen\Routing\Controller as BaseController;
use Illuminate\Http\Request;
use App\Http\Service\PointGoodsService;
use App\Http\Service\ConfigService;
use App\Http\Service\OrderService;
use App\Http\Service\PointLogService;
use App\Http\Service\PointCashLogService;
use Illuminate\Support\Facades\DB;

class PointGoodsController extends BaseController
{

    protected $request;
    protected $user;
    protected $pointGoodsService;
    protected $configService;
    protected $pointLogService;
    protected $orderService;
    protected $pointCashLogService;

    public function __construct(Request $request,
                                PointGoodsService $pointGoodsService,
                                ConfigService $configService,
                                PointLogService $pointLogService,
                                OrderService $orderService,
                                PointCashLogService $pointCashLogService)
    {
        $this->request = $request;
        $this->user = $this->request->get('user');
        $this->pointGoodsService = $pointGoodsService;
        $this->configService = $configService;
        $this->pointLogService = $pointLogService;
        $this->orderService = $orderService;
        $this->pointCashLogService = $pointCashLogService;
    }

    public function index()
    {
        $appSlider = $this->configService->findFirstRowByFields(['type' => 'point_slider']);
        $slider = $appSlider['content'];

        $list = $this->pointGoodsService->list();

        return toJSON(['slider' => $slider, 'list' => $list]);
    }


    public function detail()
    {
        $id = $this->request->input('id', 0);
        return toJSON($this->pointGoodsService->getDetail($id));
    }

    public function order()
    {
        $id     = $this->request->input('id');
        $params = $this->request->input();

        $pointGoods = $this->pointGoodsService->find($id);

        if($pointGoods == null) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '积分商品不存在');
        }

        if ($pointGoods->points > $this->user->points) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '您的积分不足，请多多积累积分哦');
        }

        if ($pointGoods->number == 0) {
            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '刚刚被人抢走了，下次还有机会');
        }

        $affectedRows = DB::update('UPDATE `mm_point_goods` SET `number` = `number`-1 where `id`=' . $id . ' AND `number` > 0 LIMIT 1');
        if ($affectedRows == 0) {

            return toJSON('', \Constant::NEED_SHOW_MESSAGE, '刚刚被人抢走了，下次还有机会');
        }

        $address = json_encode($params);

        $order = $this->orderService->create([
            'user_id' => $this->user->id,
            'sn' => generateOrderSn(),
            'status' => 1,
            'foreign_id' => $id,
            'type' => 2,
            'goods_id' => $pointGoods->goods_id,
            'goods_number' => 1,
            'address' => $address,
        ]);

        //收货地址更新
        $this->user->address = $address;
        $this->user->save();

        //用户积分改变
        $this->user->points -= $pointGoods->points;
        $this->user->save();

        //积分日志
        $this->pointLogService->create(
            [
                'user_id' => $this->user->id,
                'number' => -$pointGoods->points,
                'change_desc' => '兑换' . $pointGoods->id . '-' . $pointGoods->name,
                'change_type' => 2
            ]
        );

        return toJSON($this->user);
    }

    public function log()
    {
        $page = $this->request->input('page', 1);
        return toJSON($this->pointLogService->log($this->user, $page));
    }

    // 积分提现
    public function cash()
    {
        $alipay = $this->request->input('alipay', '');
        $realName = $this->request->input('real_name', '');
        $number = $this->request->input('number', 0);

        if (empty($alipay) || empty($realName) || $number == 0) {
            toJSON('', \Constant::FAILED);
        }

        if ($number > $this->user->points) {
            return toJSON('', \Constant::FAILED, '积分提现数量不能大于当前积分');
        }

        if ($number%10 != 0) {
            return toJSON('', \Constant::FAILED, '积分提现数量请为10的倍数');
        }

        DB::beginTransaction();
        try {

            $this->pointCashLogService->create(
                [
                    'user_id' => $this->user->id,
                    'alipay' => $alipay,
                    'real_name' => $realName,
                    'number' => $number,
                    'status' => 1
                ]
            );

            $this->user->points = $this->user->points - $number;
            $this->user->save();

            //积分日志
            $this->pointLogService->create(
                [
                    'user_id' => $this->user->id,
                    'number' => -$number,
                    'change_desc' => '积分提现',
                    'change_type' => 23
                ]
            );

            DB::commit();
        } catch (Exception $e) {
            DB::rollback();
            return toJSON('', \Constant::FAILED, '出现未知错误，请联系管理员');
        }

        return toJSON($this->user);
    }

    // 积分提现列表
    public function cashList()
    {
        $page = $this->request->input('page', 1);
        return toJSON($this->pointCashLogService->list($this->user, $page));
    }



}
